
package com.axc.gallery2.view;

import java.io.File;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.axc.gallery2.R;
import com.axc.gallery2.dbhelper.AlbumInfo;

public class AlbumAdapter extends ImageAdapter {




	private List<AlbumInfo> albums;
	private Context context;

	public AlbumAdapter(Context context, List<AlbumInfo> albums) {
		super(context, null);
		this.setAlbums(albums);
		this.setContext(context);
	}

	@Override
	public int getCount() {
		return this.albums.size();
	}

	@Override
	public Object getItem(int position) {
		return this.albums.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void editItem(int albumid, Object item) {
		int i =0;
		for (AlbumInfo aInfo : albums) {
			if(aInfo.getAlbumId()==albumid){
				aInfo = (AlbumInfo) item;
				editPositon = i;
				return;
			}
			i++;
		}

		notifyDataSetChanged();
	}

	@Override
	public void add(Object item) {
		AlbumInfo albumInfo = (AlbumInfo) item;
		System.out.println("add1:"+albums.size());
		albums.add(albumInfo);
		System.out.println("add2:"+albums.size());
		System.out.println("albumInfoadd"+albumInfo.getAlbumId()+albumInfo.getAlbumName()+albumInfo.getDescription());
	}

	@Override
	public void removeAt(int albumid) {
		int i = 0;
		for (AlbumInfo albumInfo : albums) {
			if(albumInfo.getAlbumId() == albumid){
				System.out.println("albums.remove1:"+albums.size());
				albums.remove(albumInfo);
				convertviewList.remove(i);
				System.out.println("albums.remove2:"+albums.size());
				notifyDataSetChanged();
				return;
			}
			i++;
		}
	}

	List<SoftReference<View>> convertviewList =new ArrayList<SoftReference<View>>();

	@SuppressWarnings("finally")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		System.out.println("convertviewList1:"+convertviewList.size()+"position:"+position);
		try {
			System.out.println("editPositon:"+editPositon);
			if(editPositon==position)
			{
				throw new IndexOutOfBoundsException();
			}
			convertView = convertviewList.get(position).get();
		} catch (IndexOutOfBoundsException e) {
			System.out.println("convertView==null");
			viewHolder =new ViewHolder();
			convertView = LayoutInflater.from(this.getContext())
			.inflate(R.layout.album_listview_item, null);
			convertView.setTag(viewHolder);
			System.out.println("albums.size:" + albums.size());
			AlbumInfo album = this.albums.get(position);
			System.out.println("albumItems.size:" + album.getAlbumItems().size());
			viewHolder.imageView = (ImageView) convertView.findViewById(R.id.albumImageView);
			if (album.getAlbumItems().size() > 0 && new File(album.getAlbumItems().get(0).image_path).exists()) {
				// System.out.println("location:" + album.getAlbumItems().get(position)+"exists:"+new File(album.getAlbumItems().get(0)).exists());
				//Bitmap bitmap = SampleSize.getSampleSize(album.getAlbumItems().get(0));
				new AsyncLoadImage().execute(new Object[]{viewHolder.imageView,album.getAlbumItems().get(0).image_path});
			} 
			System.out.println("1");
			viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.txtAlbumName);
			viewHolder.nameTextView.setText(album.getAlbumName());
			viewHolder.drTextView = (TextView) convertView.findViewById(R.id.txtAlbumDescription);
			viewHolder.drTextView.setText(album.getDescription());
			viewHolder.countTextView = (TextView) convertView.findViewById(R.id.txtAlbumCount);
			viewHolder.countTextView.setText(String.valueOf(album.getAlbumItems().size()));
			viewHolder.idTextView = (TextView) convertView.findViewById(R.id.txtAlbumID);
			viewHolder.idTextView.setText(String.valueOf(album.getAlbumId()));
			System.out.println("2");
			if (album.getAlbumId() == AlbumInfo.LOCALALBUMID) {// 安装判断是否已安装sdcard
				if (!Environment.getExternalStorageDirectory().exists()) {
					convertView.setEnabled(false);
				}
			}
			System.out.println("3");
			if(editPositon!=position){
				convertviewList.add(new SoftReference<View>(convertView));
			}
			else {
				convertviewList.set(position,new SoftReference<View>(convertView));
				editPositon=-1;
			}
			System.out.println("convertviewList2:"+convertviewList.size());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			return convertView;
		}
	}

	public void setAlbums(List<AlbumInfo> albums) {
		this.albums = albums;
	}

	public void clearConvaerView() {
		convertviewList.clear();
	}
	
	public AlbumInfo getAlbum(int albumId) {
		List<AlbumInfo> albumList = this.getAlbums();
		for (int i = 0; i < albumList.size(); i++) {
			if (albumList.get(i).getAlbumId() == albumId) {
				return albumList.get(i);
			}
		}
		return null;

	}

	public List<AlbumInfo> getAlbums() {
		return albums;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public Context getContext() {
		return context;
	}

}
